package handlers

import (
	"chirp-read/cache"
	"chirp-read/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func DepotAdd(c *gin.Context) {
	depot_code := c.PostForm("depot_code")
	depot_name := c.PostForm("depot_name")
	depot_type := c.PostForm("depot_type")
	if depot_code == "" || depot_name == "" || depot_type == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Depot{
		Model:     gorm.Model{},
		DepotCode: depot_code,
		DepotName: depot_name,
		DepotType: depot_type,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func DepotOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	depot_id, _ := strconv.Atoi(c.PostForm("depot_id"))
	if order_code == "" || order_price == 0 || depot_id == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		Model:      gorm.Model{},
		OrderCode:  order_code,
		DepotId:    depot_id,
		OrderPrice: order_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func DepotOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var depotOrder []models.DepotOrderList
	redis_key := "depot_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_price as OrderPrice",
				"depots.depot_code as DepotCode",
				"depots.depot_name as DepotName",
				"depots.depot_type as DepotType",
			).
			Joins("left join depots on depots.id=orders.depot_id").
			Offset(offset).Limit(size).Find(&depotOrder)
		jsonData, _ := json.Marshal(depotOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &depotOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": depotOrder,
	})
}
func DepotOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var depotOrder models.DepotOrderList
	redis_key := "depot_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_price as OrderPrice",
				"depots.depot_code as DepotCode",
				"depots.depot_name as DepotName",
				"depots.depot_type as DepotType",
			).
			Joins("left join depots on depots.id=orders.depot_id").
			Where("orders.id = ?", id).Find(&depotOrder)
		jsonData, _ := json.Marshal(depotOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &depotOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": depotOrder,
	})
}
